/**
 * Created by xp on 15/10/15.
 */


$(function(){
    $(".contentInput").val($(".listContainer:first").find("div").children("input").val());
    $(".titleInput").val($(".titleCard:first").find("p").text());
    $(".titleInput").attr("data-id",$(".listContainer:first").find("div").attr("data-id"));

    $("#edit").on("click",function(){
        $(this).css("display","none");
        $("#save").css("display","inline-block");
        $("#delete").css("display","inline-block");
        $("#addsave").css("display","none");
        $(".contentInput").removeAttr("readonly");
        $(".titleInput").removeAttr("readonly");
    })
    $("#addsave").on("click",function() {
        var title = $(".titleInput").val();
        var content = $(".contentInput").val();
        $.ajax({
            url: "/notebook/add",
            type: "POST",
            data: {
                title: title,
                content: content,
            },
            dataType: "json",
            success: function (data) {
                location.reload();
            }
        });
    }); //addsava事件

    $("#save").on("click",function() {
        var title = $(".titleInput").val();
        var content = $(".contentInput").val();
        var id = $(".titleInput").attr("data-id");

        $.ajax({
            url: "/notebook/update",
            type: "POST",
            data: {
                noteId: id,
                title: title,
                content: content,
            },
            dataType: "json",
            success: function (data) {
                location.reload();
            }
        });
    }); //editsava事件

    $("#delete").on("click",function() {
        var id = $(".titleInput").attr("data-id");
        $.ajax({
            url: "/notebook/remove",
            type: "GET",
            data: {
                noteId: id
            },
            dataType: "json",
            success: function (data) {
                location.reload();
            }
        });
    }); //delete事件

    $("#btnSearch").on("click",function(){
        var keyword = $("#search").val();
        $.ajax({
            url: "/notebook/search",
            type: "GET",
            data: {
                key: keyword
            },
            dataType: "json",
            success: function (notes) {
                $(".titleCard").css("display","none");
                for(var i=0;i<notes.length;i++){
                    var titleCardId = 'a'+notes[i].id;
                    $("#"+titleCardId+"").css("display","block");
                }
            }
        });
    }); //搜索事件

    $("#add").on("click",function(){
        $(".contentInput").val("").removeAttr("readonly");
        $(".titleInput").val("").removeAttr("readonly");
        $("#addsave").css("display","inline-block");
        $("#delete").css("display","none");
        $("#save").css("display","none");
        $("#edit").css("display","none");

    })



});



function cardClick(self){
    var content = $(self).children("input").val();
    var title = $(self).children("p").text();
    var noteid= $(self).attr("data-id");
    $(".titleInput").attr("data-id",noteid);
    $(".contentInput").val(content);
    $(".titleInput").val(title);
    $("#edit").css("display","block");
    $("#addsave").css("display","none");
    $("#delete").css("display","none");
    $("#save").css("display","none");
}//标题点击

